using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Curchy.PERTToolbox.WinUI;
using Curchy.PERTToolbox.Service;
using Curchy.PERTToolbox.Entity;
using Curchy.PERTToolbox.Properties;

namespace Curchy.PERTToolbox.WinUI
{
    public partial class Master : Form
    {
        private bool _closeCalled = false;
        
        public event EventHandler Minimize;

        public Master()
        {
            InitializeComponent();

            this.Minimize += new EventHandler(Master_Minimize);
        }
        
        public Master(string filename)
        {
            InitializeComponent();

            this.Minimize += new EventHandler(Master_Minimize);

            Project prj = PersistanceToolbox<Project>.Load(filename);
            ProjectDefinition pd = new ProjectDefinition(prj);
            pd.MdiParent = this;
            pd.Show();
        }

        private void Master_Minimize(object sender, EventArgs e)
        {
 	        if(Settings.Default.MinimizeToTray)
                this.Visible = false;
        }

        private void Master_Load(object sender, EventArgs e)
        {
        }

        private void Master_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.Save();
            if (Settings.Default.CloseToTray && !_closeCalled)
            {
                e.Cancel = true;
                this.Visible = false;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _closeCalled = true;
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About ab = new About();
            ab.MdiParent = this;
            ab.Show();            
        }

        private void projecttoolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProjectDefinition pd = new ProjectDefinition();
            pd.MdiParent = this;
            pd.Show();
        }

        private void openProjecttoolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            DialogResult result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                Project prj = PersistanceToolbox<Project>.Load(openFileDialog.FileName);
                ProjectDefinition pd = new ProjectDefinition(prj);
                pd.MdiParent = this;
                pd.Show();
            }
        }

        private void defaultCalendarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SystemSettingsEntry ss = new SystemSettingsEntry();
            ss.MdiParent = this;
            ss.Show();
        }

        protected override void WndProc(ref Message msg) 
        {
            const int WM_SIZE = 0x0005;
            const int SIZE_MINIMIZED = 1;

            if (
                    (msg.Msg == WM_SIZE) && 
                    ((int)msg.WParam == SIZE_MINIMIZED) && 
                    (this.Minimize != null)
                )
                this.Minimize(this, EventArgs.Empty);

            base.WndProc(ref msg);
        }


        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void notQuitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _closeCalled = true;
            Application.Exit();
        }

    }
}